﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Client_Server_Interface;

namespace BTM_Client_Application.RealTimeFilter
{
    /// <summary>
    /// Логика взаимодействия для RealTimeFilterControl.xaml
    /// </summary>
    public partial class RealTimeFilterControl
    {
        public delegate void FilterUpdatedDelegate(FilterElementControl filter);

        //private readonly ObservableCollection<ErrorList.ErrorListRecordImpl> _errorList;
        //private ObservableCollection<ErrorList.ErrorListRecordImpl> FilteredItems { get; set; }
        public ObservableCollection<DataGridRow> FilteredRows { get; private set; }
        private readonly Dictionary<string, FilterElementControl> _filters; 


        public RealTimeFilterControl(Orientation orientation = Orientation.Horizontal)
        {
            InitializeComponent();
            _filters = new Dictionary<string, FilterElementControl>();

            //FilteredItems = new ObservableCollection<ErrorList.ErrorListRecordImpl>();
            FilteredRows = new ObservableCollection<DataGridRow>();
            //var l = new ListView();
        }
        /*
        /// <summary>
        /// 
        /// </summary>
        /// <param name="errList">Ссылка на список текущих ошибок, из которого фильруются элементы.</param>
        public RealTimeFilterControl(ref ObservableCollection<ErrorList.ErrorListRecordImpl> errList)
            : this()
        {
            _errorList = errList;
        }
        /// <summary>
        /// Консруктор, который создает элементы фильтра.
        /// </summary>
        /// <param name="filterObject">Объект, по интерфейсу которого создаются элементы фильтра.</param>
        /// <param name="errList">Ссылка на список текущих ошибок, из которого фильруются элементы.</param>
        public RealTimeFilterControl(IRealTimeFilterObject filterObject, ref ObservableCollection<ErrorList.ErrorListRecordImpl> errList)
            : this(ref errList)
        {
            foreach (var elementType in filterObject.FilterInterface)
            {
                var filter = new FilterElementControl(elementType.Key, elementType.Value, On_UpdatedFilter);
                _filters.Add(elementType.Key, filter);
                ElementsPanel.Children.Add(filter);
            }
        }

        
        /// <summary>
        /// Добавление нового элемента фильтра.
        /// </summary>
        /// <param name="title">Название элемента.</param>
        /// <param name="filterInitializer">Инициализатор элемента.</param>
        public void AddFilterElement(string title, FilterElementInitializer filterInitializer)
        {
            var filter = new FilterElementControl(title, filterInitializer, On_UpdatedFilter);
            _filters.Add(title, filter);
            AddFilterElement(filter);
            //ElementsPanel.Children.Add(filter);
        }
         */

        public void AddFilterElement(FilterElementControl filter)
        {
            ElementsPanel.Children.Add(filter);
        }
        /*
        public bool CheckElement(IRealTimeFilterObject elementToCheck)
        {
            bool ckd = true;
            foreach (var element in elementToCheck.FilterElements)
            {
                if(_filters[element.Key] == null)
                    continue;
                if (!_filters[element.Key].CheckElement(element.Value))
                {
                    ckd = false;
                    break;
                }
            }
            return ckd;
        }

        public void On_NewError(ErrorList.ErrorListRecordImpl record)
        {
            if (CheckElement(record))
            {
                FilteredRows.Add(new DataGridRow {Item = record});
                //FilteredItems.Add(record);
            }
        }

        public void On_RemoveError(ErrorListRecord record)
        {
            var ier = FilteredRows.Single(x => Equals(x.Item, record));
            FilteredRows.Remove(ier);
            //var r = FilteredItems.Single(x => x.GetRecordId() == record.GetRecordId());
            //FilteredItems.Remove(r);
        }

        public void On_UpdatedFilter(FilterElementControl updatedFilter)
        {
            //FilteredItems.Clear();
            FilteredRows.Clear();
            foreach (var error in _errorList)
            {
                if (CheckElement(error))
                {
                    //FilteredItems.Add(error);
                    FilteredRows.Add(new DataGridRow {Item = error});
                }
            }
        }
         */
    }
}
